package com.liziguo.lzgfp.server.controller;

import com.liziguo.lzgfp.server.domain.ClientView;
import com.liziguo.lzgfp.server.domain.Result;
import com.liziguo.lzgfp.server.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Author: Liziguo
 * @Date: 2021/12/25 21:21
 */
@Controller
public class AdminController {

    @Autowired
    private AdminService adminService;

    @GetMapping("/")
    public String index(ModelMap modelMap) {
        List<ClientView> list = adminService.list();
        modelMap.put("clients", list);
        modelMap.put("onLineCount", list.stream().filter(ClientView::isOnline).count());
        return "index";
    }

    @PostMapping("/get")
    @ResponseBody
    public Result get(String key) {
        return adminService.getView(key);
    }

    @PostMapping("/update")
    @ResponseBody
    public Result update(String key, String remark, String forwards) {
        return adminService.update(key, remark, forwards);
    }

    @PostMapping("/insert")
    @ResponseBody
    public Result insert(String key, String remark, String forwards) {
        return adminService.insert(key, remark, forwards);
    }

    @PostMapping("/delete")
    @ResponseBody
    public Result delete(String key) {
        return adminService.delete(key);
    }

    @PostMapping("/disconnect")
    @ResponseBody
    public Result disconnect(String key, int s) {
        return adminService.disconnect(key, s);
    }

    @PostMapping("/forcedDisconnect")
    @ResponseBody
    public Result forcedDisconnect(String key) {
        return adminService.forcedDisconnect(key);
    }

    @RequestMapping("/download-client")
    public ResponseEntity<byte[]>downloadClient(){
        return adminService.downloadClient();
    }
}
